var pieceMatrixIteratorTestCase = {};

var PieceMatrixIterator = it.koutifaris._tetris.PieceMatrixIterator;
var rotationSystem = new it.koutifaris._tetris.SRSRotationSystem();
var PieceType = it.koutifaris._tetris.PieceType;

pieceMatrixIteratorTestCase.getTestCaseName = function() {
	return "PieceMatrixIterator Test";
};

pieceMatrixIteratorTestCase.testHasNextOnIPiece = function() {
	var iMatrix = rotationSystem.getPieceMatrix(PieceType.I, 0);
	var iterator = new PieceMatrixIterator(iMatrix, 0, 0);

	for ( var i = 0; i < 4; i++) {
		assertTrue(iterator.hasNext(), i + " cycle.");
		iterator.next();
	}

	assertFalse(iterator.hasNext(), "No more iterations.");
};

pieceMatrixIteratorTestCase.testIPiece = function() {
	var iMatrix = rotationSystem.getPieceMatrix(PieceType.I, 0);
	var iterator = new PieceMatrixIterator(iMatrix, 0, 0);

	for ( var i = 0; i < 4; i++) {
		var squarePosition = iterator.next();
		assertEquals(1, squarePosition.r);
		assertEquals(i, squarePosition.c);
	}

};

pieceMatrixIteratorTestCase.testJPiece = function() {
	var jMatrix = rotationSystem.getPieceMatrix(PieceType.J, 0);
	var iterator = new PieceMatrixIterator(jMatrix, 0, 0);

	var squarePosition;

	squarePosition = iterator.next();
	assertEquals(0, squarePosition.r, "([0],0)");
	assertEquals(0, squarePosition.c, "(0,[0])");

	squarePosition = iterator.next();
	assertEquals(1, squarePosition.r, "([1],0)");
	assertEquals(0, squarePosition.c, "(1,[0])");

	squarePosition = iterator.next();
	assertEquals(1, squarePosition.r, "([1],1)");
	assertEquals(1, squarePosition.c, "(1,[1])");

	squarePosition = iterator.next();
	assertEquals(1, squarePosition.r, "([1],2)");
	assertEquals(2, squarePosition.c, "(1,[2])");

	assertFalse(iterator.hasNext());
};

pieceMatrixIteratorTestCase.testLPiece = function() {
	var lMatrix = rotationSystem.getPieceMatrix(PieceType.L, 0);
	var iterator = new PieceMatrixIterator(lMatrix, 0, 0);

	var squarePosition;

	squarePosition = iterator.next();
	assertEquals(0, squarePosition.r, "([0],2)");
	assertEquals(2, squarePosition.c, "(0,[2])");

	squarePosition = iterator.next();
	assertEquals(1, squarePosition.r, "([1],0)");
	assertEquals(0, squarePosition.c, "(1,[0])");

	squarePosition = iterator.next();
	assertEquals(1, squarePosition.r, "([1],1)");
	assertEquals(1, squarePosition.c, "(1,[1])");

	squarePosition = iterator.next();
	assertEquals(1, squarePosition.r, "([1],2)");
	assertEquals(2, squarePosition.c, "(1,[2])");

	assertFalse(iterator.hasNext());
};

pieceMatrixIteratorTestCase.testOPiece = function() {
	var oMatrix = rotationSystem.getPieceMatrix(PieceType.O, 0);
	var iterator = new PieceMatrixIterator(oMatrix, 0, 0);

	var squarePosition;

	squarePosition = iterator.next();
	assertEquals(0, squarePosition.r, "([0],0)");
	assertEquals(0, squarePosition.c, "(0,[0])");

	squarePosition = iterator.next();
	assertEquals(0, squarePosition.r, "([0],1)");
	assertEquals(1, squarePosition.c, "(0,[1])");

	squarePosition = iterator.next();
	assertEquals(1, squarePosition.r, "([1],0)");
	assertEquals(0, squarePosition.c, "(1,[0])");

	squarePosition = iterator.next();
	assertEquals(1, squarePosition.r, "([1],1)");
	assertEquals(1, squarePosition.c, "(1,[1])");

	assertFalse(iterator.hasNext());
};

pieceMatrixIteratorTestCase.testSPiece = function() {
	var sMatrix = rotationSystem.getPieceMatrix(PieceType.S, 0);
	var iterator = new PieceMatrixIterator(sMatrix, 0, 0);

	var squarePosition;

	squarePosition = iterator.next();
	assertEquals(0, squarePosition.r, "([0],1)");
	assertEquals(1, squarePosition.c, "(0,[1])");

	squarePosition = iterator.next();
	assertEquals(0, squarePosition.r, "([0],2)");
	assertEquals(2, squarePosition.c, "(0,[2])");

	squarePosition = iterator.next();
	assertEquals(1, squarePosition.r, "([1],0)");
	assertEquals(0, squarePosition.c, "(1,[0])");

	squarePosition = iterator.next();
	assertEquals(1, squarePosition.r, "([1],1)");
	assertEquals(1, squarePosition.c, "(1,[1])");

	assertFalse(iterator.hasNext());
};

pieceMatrixIteratorTestCase.testTPiece = function() {
	var tMatrix = rotationSystem.getPieceMatrix(PieceType.T, 0);
	var iterator = new PieceMatrixIterator(tMatrix, 0, 0);

	var squarePosition;

	squarePosition = iterator.next();
	assertEquals(0, squarePosition.r, "([0],1)");
	assertEquals(1, squarePosition.c, "(0,[1])");

	squarePosition = iterator.next();
	assertEquals(1, squarePosition.r, "([1],0)");
	assertEquals(0, squarePosition.c, "(1,[0])");
	
	squarePosition = iterator.next();
	assertEquals(1, squarePosition.r, "([1],1)");
	assertEquals(1, squarePosition.c, "(1,[1])");
	
	squarePosition = iterator.next();
	assertEquals(1, squarePosition.r, "([1],2)");
	assertEquals(2, squarePosition.c, "(0,[2])");


	assertFalse(iterator.hasNext());
};

pieceMatrixIteratorTestCase.testZPiece = function() {
	var zMatrix = rotationSystem.getPieceMatrix(PieceType.Z, 0);
	var iterator = new PieceMatrixIterator(zMatrix, 0, 0);

	var squarePosition;

	squarePosition = iterator.next();
	assertEquals(0, squarePosition.r, "([0],0)");
	assertEquals(0, squarePosition.c, "(0,[0])");

	squarePosition = iterator.next();
	assertEquals(0, squarePosition.r, "([0],1)");
	assertEquals(1, squarePosition.c, "(0,[1])");
	
	squarePosition = iterator.next();
	assertEquals(1, squarePosition.r, "([1],1)");
	assertEquals(1, squarePosition.c, "(1,[1])");
	
	squarePosition = iterator.next();
	assertEquals(1, squarePosition.r, "([1],2)");
	assertEquals(2, squarePosition.c, "(0,[2])");


	assertFalse(iterator.hasNext());
};